//
//  DeviceCollectionViewCell.m
//  YSSmartHome
//
//  Created by Yahaong on 16/8/11.
//  Copyright © 2016年 yahaong. All rights reserved.
//

#import "DeviceCollectionViewCell.h"
#import "MultipleShowView.h"
#import "ZTLCCamera.h"
#import "CellBottomView.h"
#import "ZTNoControlDevice.h"
#import "ZTOneControlDevice.h"
#import "ZTTwoControlDevice.h"
#import "ZTThreeControlDevice.h"
#import "ZTFourControlDevice.h"
#import "ZTAirMonitorDevice.h"

#import "XAIGateway.h"

@interface DeviceCollectionViewCell ()
<
CellBottomViewDelegate
>
@property (nullable,nonatomic, strong) CellBottomView* bottomView;
@property (nullable,nonatomic, strong) UIView* topView;
@property (nullable,nonatomic, strong) UIImageView* iconView;
@property (nullable,nonatomic, strong) UILabel*     nameLabel;
@property (nullable,nonatomic, strong) UIImageView* netStatusView;
@property (nullable,nonatomic, strong) MultipleShowView *multipleShowView;
@end

@implementation DeviceCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initSubviews];
        self.clipsToBounds = true;
    }
    return self;
}


#pragma mark - CellBottomViewDelegate

- (void)cellottomView:(CellBottomView* __nonnull)view clickType:(CellBottomViewClickType)clickType
{
    if (clickType == CellBottomViewClickTypeEdit)
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"kZTHomeEditDeviceNotifKey" object:nil];
    }
    else
    {
        [self removeDevice];
    }
}


#pragma mark - private method
- (void)initSubviews
{
    self.contentView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.bottomView];
    [self.contentView addSubview:self.topView];
    [self.topView addSubview:self.nameLabel];
    [self.topView addSubview:self.iconView];
    [self.topView addSubview:self.multipleShowView];
    [self.contentView addSubview:self.netStatusView];
    [self layoutPageSubviews];
}
- (void)layoutPageSubviews
{
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.bottom.equalTo(self.contentView);
        make.height.equalTo(0);
    }];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.equalTo(self.contentView);
        make.bottom.equalTo(self.bottomView.top);
        make.height.equalTo(self.contentView);
    }];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.topView).offset(kIphoneSize_Height(-16));
        make.centerX.equalTo(self.contentView);
        make.leading.trailing.equalTo(self.contentView).offset(UIEdgeInsetsMake(0, 10, 0, 10));
    }];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.nameLabel.top).offset(kIphoneSize_Height(-8));
        make.centerX.equalTo(self.topView);
        make.size.equalTo(CGSizeMake(kIphoneSize_Widith(50), kIphoneSize_Height(42)));
    }];
    [self.multipleShowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.iconView.top).offset(-8);
        make.centerX.equalTo(self.topView);
        make.size.equalTo(CGSizeMake(32, 8));
    }];
    [self.netStatusView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.equalTo(5);
        make.size.equalTo(CGSizeMake(kIphoneSize_Widith(25), kIphoneSize_Height(22)));
    }];
}

- (void)showBottomView
{
    [self.bottomView updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(kIphoneSize_Height(30));
    }];
    [self.nameLabel updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.topView).offset(kIphoneSize_Height(-8));
    }];
    [self.iconView updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.nameLabel.top).offset(kIphoneSize_Height(-4));
    }];
    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}
- (void)cellRecover
{
    [self.bottomView updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(kIphoneSize_Height(0));
    }];
    [self.nameLabel updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.topView).offset(kIphoneSize_Height(-16));
    }];
    [self.iconView updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.nameLabel.top).offset(kIphoneSize_Height(-8));
    }];

    [UIView animateWithDuration:0.25 animations:^{
        [self layoutIfNeeded];
    }];
}


- (void)removeDevice
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"kZTRemoveDeviceNotifKey" object:nil];
}

#pragma mark - getters and setters
- (void)setDev:(ZTDevice *)dev
{
    _dev = dev;
    self.nameLabel.text = [dev getDeviceName];
    BOOL isOffline = dev.devStatus == ZTDeviceStatusOFFLINE || kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE;
    [self.netStatusView setHidden:!isOffline];
    //单路开关
    if (dev.devType == ZTDeviceTypeOneWay0Switch
        || dev.devType == ZTDeviceTypeOneWaySwitch) {

        self.multipleShowView.hidden = NO;
        self.multipleShowView.device = dev;
        ZTOneControlDevice *device = (ZTOneControlDevice*)dev;
        if (kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE ) {
            self.iconView.image = [UIImage imageNamed:@"home_light_offline"];
        } else {
            if (device.devStatus == ZTDeviceStatusONLINE) {
                if (device.status ) {
                    self.iconView.image = [UIImage imageNamed:@"home_light_open"];
                } else  {
                    self.iconView.image = [UIImage imageNamed:@"home_light_normal"];
                }
            } else {
                self.iconView.image = [UIImage imageNamed:@"home_light_offline"];
            }
        }
    }
    //二路开关
    else if (dev.devType == ZTDeviceTypeTwoWay0Switch
             || dev.devType == ZTDeviceTypeTwoWaySwitch)
    {
        self.multipleShowView.hidden = NO;
        self.multipleShowView.device = dev;
        ZTTwoControlDevice *device =(ZTTwoControlDevice *)dev;
        if (kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE ) {
            self.iconView.image = [UIImage imageNamed:@"home_light_offline"];
        } else  {
            if (device.devStatus == ZTDeviceStatusONLINE ) {
                if (device.oneStatus || device.twoStatus)  {
                    self.iconView.image = [UIImage imageNamed:@"home_light_open"];
                }  else {
                    self.iconView.image = [UIImage imageNamed:@"home_light_normal"];
                }
            } else {
                self.iconView.image = [UIImage imageNamed:@"home_light_offline"];
            }
        }
    }
    //三路开关
    else if (dev.devType == ZTDeviceTypeThreeWay0Switch
             || dev.devType == ZTDeviceTypeThreeWaySwitch)
    {
        self.multipleShowView.hidden = NO;
        self.multipleShowView.device = dev;
        ZTThreeControlDevice *device =(ZTThreeControlDevice *)dev;
        if (kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE)
        {
            self.iconView.image = [UIImage imageNamed:@"home_light_offline"];
        }
        else
        {
            if (device.devStatus == ZTDeviceStatusONLINE )
            {
                if (device.oneStatus || device.twoStatus||device.threeStatus)
                {
                    self.iconView.image = [UIImage imageNamed:@"home_light_open"];
                }
                else
                {
                    self.iconView.image = [UIImage imageNamed:@"home_light_normal"];
                }
            }
            else
            {
                self.iconView.image = [UIImage imageNamed:@"home_light_offline"];
            }
            
        }
        
    }
    //四路开关
    else if (dev.devType == ZTDeviceTypeFourWay0Switch
             || dev.devType == ZTDeviceTypeFourWaySwitch)
    {
        self.multipleShowView.hidden = NO;
        self.multipleShowView.device = dev;
        ZTFourControlDevice *device = (ZTFourControlDevice*)dev;
        if (kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE )
        {
            self.iconView.image = [UIImage imageNamed:@"home_light_offline"];
        }
        else
        {
            if (device.devStatus == ZTDeviceStatusONLINE )  {
                if (device.oneStatus
                    || device.twoStatus
                    || device.threeStatus
                    || device.fourStatus)  {
                    self.iconView.image = [UIImage imageNamed:@"home_light_open"];
                }  else {
                    self.iconView.image = [UIImage imageNamed:@"home_light_normal"];
                }
            }  else {
                self.iconView.image = [UIImage imageNamed:@"home_light_offline"];
            }
        }
    }
    //一路插座
    else if (dev.devType == ZTDeviceTypeOneWaySocket) {
        self.multipleShowView.hidden = NO;
        self.multipleShowView.device = dev;
        ZTOneControlDevice *device = (ZTOneControlDevice*)dev;
        if (kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE) {
            NSArray * arrayName = [device.name componentsSeparatedByString:@"."];
            if (arrayName.count != 3) {
                self.iconView.image = [UIImage imageNamed:@"home_socket_offline_icon0"];
                return;
            }
            NSString *strIcon = [NSString stringWithFormat:@"home_socket_offline_icon%@",arrayName[2]];
            self.iconView.image = [UIImage imageNamed:strIcon];
        } else {
            if (device.devStatus == ZTDeviceStatusOFFLINE) {
                NSArray * arrayName = [device.name componentsSeparatedByString:@"."];
                if (arrayName.count != 3)  {
                    self.iconView.image = [UIImage imageNamed:@"home_socket_offline_icon0"];
                    return;
                }
                NSString *strIcon = [NSString stringWithFormat:@"home_socket_offline_icon%@",arrayName[2]];
                self.iconView.image = [UIImage imageNamed:strIcon];
            } else {
                if (device.status) {
                    NSArray<NSString *>  * arrayName = [device.name componentsSeparatedByString:@"."];
                    if (arrayName.count != 3) {
                        self.iconView.image = [UIImage imageNamed:@"home_socket_open_icon0"];
                        return;
                    }
                    NSString *strIcon  = [NSString stringWithFormat:@"home_socket_open_icon%@",arrayName[2]];
                    self.iconView.image = [UIImage imageNamed:strIcon];
                } else {
                    NSArray * arrayName = [device.name componentsSeparatedByString:@"."];
                    if (arrayName.count != 3) {
                        self.iconView.image = [UIImage imageNamed:@"home_socket_close_icon0"];
                        return;
                    }
                    NSString *strIcon = [NSString stringWithFormat:@"home_socket_close_icon%@",arrayName[2]];
                    self.iconView.image = [UIImage imageNamed:strIcon];
                }
            }
        }
    }
    //门磁
    else if (dev.devType == ZTDeviceTypeZTDoorsensor )
    {
        [self.netStatusView setHidden:YES];
        self.multipleShowView.hidden = YES;
        ZTNoControlDevice *noControlDev = (ZTNoControlDevice *)_dev;
        if (kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE )
        {
            self.iconView.image = [UIImage imageNamed:@"home_doorsensor_offline"];
        }
        else
        {
            if (noControlDev.devStatus == ZTDeviceStatusOFFLINE)
            {
                self.iconView.image = [UIImage imageNamed:@"home_doorsensor_offline"];
            }
            else if (noControlDev.devStatus == ZTDeviceStatusSleeping)
            {
                self.iconView.image = noControlDev.status ? [UIImage imageNamed:@"home_doorsensor_open_sleeping"] : [UIImage imageNamed:@"home_doorsensor_normal_sleeping"];
            }
            else
            {
                self.iconView.image = noControlDev.status ? [UIImage imageNamed:@"home_doorsensor_open"] : [UIImage imageNamed:@"home_doorsensor_normal"];
            }
        }
    }
    //乐橙摄像头
    else if (dev.devType == ZTDeviceTypeLCCamera )
    {
        self.multipleShowView.hidden = YES;
        ZTLCCamera *lcCamera = (ZTLCCamera *)_dev;
        if (kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE )
        {
            self.iconView.image = [UIImage imageNamed:@"home_camera_offline"];
        }
        else
        {
            if (lcCamera.devStatus == ZTDeviceStatusOFFLINE )
            {
                self.iconView.image = [UIImage imageNamed:@"home_camera_offline"];
            }
            else
            {
                self.iconView.image = [UIImage imageNamed:@"home_camera_online"];
            }
        }
    }
    //红外探测器
    else if (dev.devType == ZTDeviceTypeInfraredDetector )
    {
        [self.netStatusView setHidden:YES];
        self.multipleShowView.hidden = YES;
        ZTOneControlDevice *oneControlDevice = (ZTOneControlDevice *)_dev;
        if (kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE )
        {
            self.iconView.image = [UIImage imageNamed:@"home_infraredDetector_offline"];
        }
        else
        {
            if (oneControlDevice.devStatus == ZTDeviceStatusOFFLINE)
            {
                self.iconView.image = [UIImage imageNamed:@"home_infraredDetector_offline"];
            }
            else if (oneControlDevice.devStatus == ZTDeviceStatusSleeping)
            {
                self.iconView.image = oneControlDevice.status ? [UIImage imageNamed:@"home_infraredDetector_open_sleeping"] : [UIImage imageNamed:@"home_infraredDetector_normal_sleeping"];
            }
            else
            {
                self.iconView.image = oneControlDevice.status ? [UIImage imageNamed:@"home_infraredDetector_open"] : [UIImage imageNamed:@"home_infraredDetector_normal"];
            }
        }
    }
    //GAS
    else if (dev.devType == ZTDeviceTypeGAS)
    {
        self.multipleShowView.hidden = YES;
        ZTOneControlDevice *oneControlDevice = (ZTOneControlDevice *)_dev;
        if (kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE )
        {
            self.iconView.image = [UIImage imageNamed:@"home_gas_offline"];
        }
        else
        {
            if (oneControlDevice.devStatus == ZTDeviceStatusSleeping
                || oneControlDevice.devStatus == ZTDeviceStatusOFFLINE)
            {
                self.iconView.image = [UIImage imageNamed:@"home_gas_offline"];
            }
            else
            {
                self.iconView.image = oneControlDevice.status ? [UIImage imageNamed:@"home_gas_open"] : [UIImage imageNamed:@"home_gas_normal"];
            }
        }
    }
    //一键报警
    else if (dev.devType == ZTDeviceTypeAlarmkey)
    {
        self.multipleShowView.hidden = YES;
        ZTNoControlDevice *noControlDev = (ZTNoControlDevice *)_dev;
        if (kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE )
        {
            self.iconView.image = [UIImage imageNamed:@"home_alarm_offline"];
        }
        else
        {
            if (noControlDev.devStatus == ZTDeviceStatusOFFLINE)
            {
                self.iconView.image = [UIImage imageNamed:@"home_alarm_offline"];
            }
            else if (noControlDev.devStatus == ZTDeviceStatusSleeping)
            {
                self.iconView.image = noControlDev.status ? [UIImage imageNamed:@"home_alarm_open_sleeping"] : [UIImage imageNamed:@"home_alarm_normal_sleeping"];
            }
            else
            {
                self.iconView.image = noControlDev.status ? [UIImage imageNamed:@"home_alarm_open"] : [UIImage imageNamed:@"home_alarm_normal"];
            }
        }
    }
    //烟雾探测器
    else if (dev.devType == ZTDeviceTypeSmoke)
    {
        self.multipleShowView.hidden = YES;
        ZTOneControlDevice *oneControlDevice = (ZTOneControlDevice *)_dev;
        if (kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE)
        {
            self.iconView.image = [UIImage imageNamed:@"home_smoke_offline"];
        }
        else
        {
            if (oneControlDevice.devStatus == ZTDeviceStatusSleeping
                || oneControlDevice.devStatus == ZTDeviceStatusOFFLINE)
            {
                self.iconView.image = [UIImage imageNamed:@"home_smoke_offline"];
            }
            else
            {
                self.iconView.image = oneControlDevice.status ? [UIImage imageNamed:@"home_smoke_open"] : [UIImage imageNamed:@"home_smoke_normal"];
            }
        }
    }
    //空气
    else if (dev.devType == ZTDeviceTypeAir)
    {
        self.multipleShowView.hidden = YES;
        ZTAirMonitorDevice *airMonitorDevice = (ZTAirMonitorDevice *)_dev;
        if (kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE)
        {
            self.iconView.image = [UIImage imageNamed:@"home_air_offline"];
        }
        else
        {
            if (airMonitorDevice.devStatus == ZTDeviceStatusSleeping
                || airMonitorDevice.devStatus == ZTDeviceStatusOFFLINE)
            {
                self.iconView.image = [UIImage imageNamed:@"home_air_offline"];
            }
            else
            {
                self.iconView.image = airMonitorDevice.devStatus ? [UIImage imageNamed:@"home_air_open"] : [UIImage imageNamed:@"home_air_normal"];
            }
        }
    }
    else if (dev.devType == ZTDeviceTypeFlooding)
    {
        self.multipleShowView.hidden = YES;
        ZTNoControlDevice *noControlDevice = (ZTNoControlDevice *)_dev;
        if (kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE)
        {
            self.iconView.image = [UIImage imageNamed:@"home_flooding_offline"];
        }
        else
        {
            if (noControlDevice.devStatus == ZTDeviceStatusSleeping
                || noControlDevice.devStatus == ZTDeviceStatusOFFLINE)
            {
                self.iconView.image = [UIImage imageNamed:@"home_flooding_offline"];
            }
            else
            {
                self.iconView.image = noControlDevice.status ? [UIImage imageNamed:@"home_flooding_open"] : [UIImage imageNamed:@"home_flooding_normal"];
            }
        }
    }
    //单轨道窗帘
    else if (dev.devType == ZTDeviceTypeCurtainSingleTrack)
    {
        self.multipleShowView.hidden = YES;
        ZTOneControlDevice *oneControlDevice = (ZTOneControlDevice *)_dev;
        if (kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE )
        {
            self.iconView.image = oneControlDevice.status ? [UIImage imageNamed:@"home_curtain_open_offline"] : [UIImage imageNamed:@"home_curtain_normal_offline"];
        }
        else
        {
            if (oneControlDevice.devStatus == ZTDeviceStatusSleeping
                || oneControlDevice.devStatus == ZTDeviceStatusOFFLINE)
            {
                self.iconView.image = oneControlDevice.status ? [UIImage imageNamed:@"home_curtain_open_offline"] : [UIImage imageNamed:@"home_curtain_normal_offline"];
            }
            else if (oneControlDevice.devStatus == ZTDeviceStatusONLINE)
            {
                self.iconView.image = oneControlDevice.status ? [UIImage imageNamed:@"home_curtain_open"] : [UIImage imageNamed:@"home_curtain_normal"];
            }
        }
    }
    //双轨道窗帘
    else if (dev.devType == ZTDeviceTypeCurtainDoubleTrack)
    {
        self.multipleShowView.hidden = NO;
        self.multipleShowView.device = dev;
        ZTTwoControlDevice *device =(ZTTwoControlDevice *)dev;
        if (kMQTTSessionManager.currentGateway.devStatus == ZTDeviceStatusOFFLINE )
        {
            if (device.oneStatus || device.twoStatus)
            {
                self.iconView.image = [UIImage imageNamed:@"home_curtain_open_offline"];
            }
            else
            {
                self.iconView.image = [UIImage imageNamed:@"home_curtain_normal_offline"];
            }
        }
        else
        {
            if (device.devStatus == ZTDeviceStatusONLINE )
            {
                if (device.oneStatus || device.twoStatus)
                {
                    self.iconView.image = [UIImage imageNamed:@"home_curtain_open"];
                }
                else
                {
                    self.iconView.image = [UIImage imageNamed:@"home_curtain_normal"];
                }
            }
            else if(device.devStatus == ZTDeviceStatusOFFLINE
                    || device.devStatus == ZTDeviceStatusSleeping)
            {
                if (device.oneStatus || device.twoStatus)
                {
                    self.iconView.image = [UIImage imageNamed:@"home_curtain_open_offline"];
                }
                else
                {
                    self.iconView.image = [UIImage imageNamed:@"home_curtain_normal_offline"];
                }
                
            }
        }
    }
    
}

- (void)setIsEditOrDel:(BOOL)isEditOrDel
{
    if (isEditOrDel)
    {
        [self showBottomView];
    }
    else
    {
        [self cellRecover];
    }
}
- (UIImageView *)iconView
{
    if (!_iconView)
    {
        _iconView = [[UIImageView alloc]init];
        _iconView.image = [UIImage imageNamed:@"home_light_offline"];
    }
    return _iconView;
}
- (UILabel *)nameLabel
{
    if (!_nameLabel)
    {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.textAlignment = NSTextAlignmentCenter;
        _nameLabel.font = kIOS_Iphone456([UIFont systemFontOfSize:14], [UIFont systemFontOfSize:13], [UIFont systemFontOfSize:12], [UIFont systemFontOfSize:12]);
        _nameLabel.textColor = RGBColor(51, 51, 51);
    }
    return _nameLabel;
}
- (UIImageView *)netStatusView
{
    if (!_netStatusView)
    {
        _netStatusView = [[UIImageView alloc]init];
        _netStatusView.image = [UIImage imageNamed:@"netStatu"];
    }
    return _netStatusView;
}
- (MultipleShowView *)multipleShowView
{
    if (!_multipleShowView)
    {
        _multipleShowView = [[MultipleShowView alloc]init];
    }
    return _multipleShowView;
}

- (CellBottomView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[CellBottomView alloc]init];
        _bottomView.delegate = self;
    }
    return _bottomView;
}
- (UIView *)topView
{
    if (!_topView) {
        _topView = [[UIView alloc]init];
    }
    return _topView;
}
@end
